﻿@model WebApp.Models.S_dept_member

@{
    Layout = "~/Views/Shared/_DialogLayout.cshtml";
}
@using (Html.BeginForm("", "", FormMethod.Post, new { @class = "form-horizontal form-inline", id = "inputform" }))
{
    <div class="dialog-content">
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <input type="hidden" id="Id" value="@Model.Id" />
        <input type="hidden" value="@(Model.Updatetime.HasValue ? Model.Updatetime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") : "")" class="form-control datafield" data-fieldname="Updatetime" />

        <div class="row">
            <div class="col-xs-12 nopadding">
                <div class="widget-box">
                    <div class="widget-title">
                        <h5>人员信息</h5>
                    </div>
                    <div class="widget-content">
                        <table cellpadding="3" cellspacing="0" border="0" class="inputform">
                            <tr>
                                <td class="td-label"><label for="Dept_id">车间</label></td>
                                <td class="td-control">
                                    @Html.DropDownList("Dept_id", Model.Dept_id.ToString(), ViewBag.DeptSelectList as SelectList, "", new { @class = "form-control datafield", @data_fieldname = "Dept_id" })
                                </td>
                                <td class="td-label" style="width:240px;">
                                    <div id="btnupload" class="btn btn-xs btn-primary fileinput-button">
                                        <span style="font-size: 12px;">上传照片</span>
                                        <input id="uploadfile" name="uploadfile" type="file">
                                    </div>
                                    @Html.Hidden("Avatorurl", Model.Avatorurl, new { @class = "form-control datafield", data_fieldname = "Avatorurl" })
                                </td>
                            </tr>
                            <tr>
                                <td class="td-label"><label for="Sort">排序</label></td>
                                <td class="td-control">
                                    @Html.TextBox("Sort", @Model.Sort, new { @class = "form-control datafield", @data_fieldname = "Sort" })
                                </td>
                                <td class="td-label">
                                    <label>(建议尺寸:240*300px)</label>
                                </td>
                            </tr>
                            <tr>
                                <td class="td-label"><label for="Name">姓名</label></td>
                                <td class="td-control" style="width:300px;">
                                    @Html.TextBox("Name", @Model.Name, new { @class = "form-control datafield", @data_fieldname = "Name" })
                                </td>
                                <td class="td-label" rowspan="3">
                                    <div style="width: 80px; height: 100px; float:right;">
                                        <div id="uploadBox" class="uploadBox" style="width: 80px; height: 100px;">
                                            <img id="imgAvator" src="@Href("~/")Basic/OfficeMember/AvatarImage?path=@(Model.Avatorurl)" style="padding: 0; max-width:100%; max-height: 100%;" />
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td class="td-label"><label for="Job">职务</label></td>
                                <td class="td-control" style="width:300px;">
                                    @Html.TextBox("Job", @Model.Job, new { @class = "form-control datafield", @data_fieldname = "Job" })
                                </td>
                            </tr>
                            <tr>
                                <td class="td-label"><label for="Remark">工作分工</label></td>
                                <td class="td-control">
                                    @Html.TextArea("Remark", @Model.Remark, new { @class = "form-control datafield", @data_fieldname = "Remark", rows = "4" })
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="dialog-footer">
        <button id="btncancel" class="btn btn-small btn-default">取消</button>
        <button id="btnsave" class="btn btn-small btn-primary">保存</button>
    </div>

}

@section css{
    <style type="text/css">
        .uploadBox {
            border: 1px solid #ddd;
            border-radius: 5px;
            margin-top: 5px;
            width: 290px;
            height: 162px;
            overflow: hidden;
        }

        .dragEnter {
            border: 1px solid #ab377a;
            border-radius: 5px;
        }
    </style>
}

@section scripts{
    
    <script type="text/javascript">
        $(document).ready(function () {
            initValidate();
            initImageUpload();

            //保存
            $("#btnsave").click(function (evt) { evt.preventDefault(); save($(this)); });
            //取消
            $("#btncancel").click(function (evt) { evt.preventDefault(); parent.closeDialog(); return false; });
        });

        //数据验证
        function initValidate() {
            //表单验证
            $('#inputform').validate({
                rules: {
                    Dept_id: { required: true },
                    Name: { required: true, maxlength: 50 },
                    Job: { required: true, maxlength: 50 },
                    Sort: { required: true, digits: true },
                    Remark: {
                        maxlength: 200
                    }
                },
                errorPlacement: function (error, element) {
                    var container = element.parents("div.col-sm-6:eq(0)");
                    var errorContainer = container.next();
                    error.appendTo(errorContainer);
                },
                messages: {
                }
            });
        }

        //初始化图片上传控件
        function initImageUpload() {
            //上传照片
            $("#uploadfile").fileupload({
                url: baseurl + 'Basic/WorkMember/UploadFile',
                //dropZone: $("#imgAvator"),
                dataType: 'json',
                //maxFileSize: 4096,   //默认5M
                //acceptFileTypes: /(\.|\/)(bmp|jpe?g|png)$/i,
                formData: {},
                submit: function () {
                    layer.msg("正在上传...", { time: 0, icon: 16 });
                },
                dragenter: function (e) {
                    $("#uploadBox").addClass("dragEnter");
                },
                dragleave: function (e) {
                    $("#uploadBox").removeClass("dragEnter");
                },
                drop: function (e) {
                    $("#uploadBox").removeClass("dragEnter");
                },
                complete: function (e, data) {
                    layer.closeAll();
                    var ret = data.result;
                    if (ret) {
                        if (ret.Ret == "1") {
                            $.messager.alert("错误", "上传失败！" + ret.Msg, "error");
                        } else {
                            layer.closeAll();
                            $("#Avatorurl").val(ret.Fileurl);
                            $("#imgAvator").attr("src", baseurl + "Basic/WorkMember/AvatarImage?path=" + ret.Fileurl);
                        }
                    }
                },
                done: function (e, data) {
                    //console.log(data);
                    layer.closeAll();
                    var ret = data.result;
                    if (ret.Ret == "1") {
                        $.messager.alert("错误", "上传失败！" + ret.Msg, "error");
                    }
                    else {
                        layer.closeAll();
                        $("#Avatorurl").val(ret.Fileurl);
                        $("#imgAvator").attr("src", baseurl + "Basic/WorkMember/AvatarImage?path=" + ret.Fileurl);
                    }
                },
                fail: function (xhr, ajaxOptions, thrownError) {
                    layer.closeAll();
                    $.messager.alert("错误", "上传失败！", "error");
                }
            });
        }

        function save(obj) {
            if (!$("#inputform").valid()) {
                return;
            }

            var jsondata = {
                Id: $("#Id").val(),
                Values: {}
            };

            //主表数据
            $("#inputform .datafield").each(function () {
                jsondata.Values[$(this).attr("data-fieldname")] = $(this).val();
            });
            //禁用保存按钮，防止重复提交
            $(obj).attr("disabled", true);

            $.ajax({
                type: "post",
                url: baseurl + "Basic/WorkMember/Save",
                data: { jsondata: JSON.stringify(jsondata) },
                success: function (e) {
                    $(obj).attr("disabled", false);
                    if (e.Ret == "0") {
                        parent.closeDialog(true);
                    } else {
                        alert(e.Msg);
                    }
                }
            });
        }

    </script>
}
